<template>
  <scroll-view
    @scrolltolower="handleToLower"
    class="recommends_view"
    scroll-y
    v-if="recommends.length > 0"
  >
    <!-- 推荐 -->
    <view class="recommend_wrap">
      <navigator
        class="recommend_item"
        v-for="item in recommends"
        :key="item.id"
        :url="`/pages/album/index?id=${item.target}`"
      >
        <image mode="widthFix" :src="item.thumb"></image>
      </navigator>
    </view>
    <!-- 月份 -->
    <view class="months_wrap">
      <view class="months_title">
        <view class="months_title_info">
          <view class="months_info">
            <text>{{ months.DD }} / </text>
            {{ months.MM }} 月
          </view>
          <view class="months_text">{{ months.title }}</view>
        </view>
        <view class="months_title_more">更多 ></view>
      </view>
      <view class="months_content">
        <view class="months_item" v-for="item in months.items" :key="item.id">
          <!-- 父传子 -->
          <go-detail :list="monthes.items" :index="index">
            <image
              mode="aspectFill"
              :src="item.thumb + item.rule.replace('$<Height>', 360)"
            ></image>
          </go-detail>
        </view>
      </view>
    </view>
    <!-- 热门 -->
    <view class="hots_wrap">
      <view class="hots_title">
        <text>热门</text>
      </view>
      <view class="hots_content">
        <view class="hots_item" v-for="(item, index) in hots" :key="item.id">
          <go-detail :list="hots" :index="index">
            <image mode="widthFix" :src="item.thumb"></image>
          </go-detail>
        </view>
      </view>
    </view>
  </scroll-view>
</template>

<script>
import moment from "moment";
import goDetail from "@/components/goDetail";
export default {
  components: {
    goDetail,
  },
  data() {
    return {
      // 推荐列表
      recommends: [],
      // 月份
      months: {},
      // 热门
      hots: [],
      // 请求的参数
      params: {
        // 获取多少条数据
        limit: 30,
        // 关键字
        order: "hot",
        // 跳过多少条
        skip: 0,
      },
      // 是否还有下一页
      hasMore: true,
    };
  },
  mounted() {
    this.getList();
  },
  methods: {
    // 获取接口的数据
    async getList() {
      const { res } = await this.request({
        url: "http://157.122.54.189:9088/image/v3/homepage/vertical",
        data: this.params,
      });
      if (res.vertical === 0) {
        this.hasMore = false;
        this.showToast();
        return;
      }
      if (this.recommends.length === 0) {
        // 第一次发送的请求
        // 推荐模块
        this.recommends = res.homepage[1].items;
        // 月份模块
        this.months = res.homepage[2];
        // 改造时间戳
        this.months.MM = moment(this.months.stime).format("MM");
        this.months.DD = moment(this.months.stime).format("DD");
      } else {
        // 获取热门数据列表
        // 数组拼接 es6
        this.hots = [...this.hots, ...res.vertical];
      }
    },
    // 滚动条触底事件
    handleToLower() {
      if (this.hasMore) {
        this.params.skip += this.params.limit;
        this.getList();
      } else {
        this.showToast();
      }
    },
  },
};
</script>

<style lang="scss" scoped>
.recommends_view {
  // 减号两边必须有空格
  height: calc(100vh - 36px);
}
.recommend_wrap {
  display: flex;
  flex-wrap: wrap;
  .recommend_item {
    width: 50%;
    border: 5rpx solid #fff;
  }
}
.months_wrap {
  .months_title {
    display: flex;
    justify-content: space-between;
    padding: 20rpx;
    .months_title_info {
      color: $color;
      font-size: 30rpx;
      font-weight: 600;
      display: flex;
      .months_info {
        text {
          font-size: 36rpx;
        }
      }
      .months_text {
        font-size: 34rpx;
        color: #666;
        margin-left: 30rpx;
      }
    }
    .months_title_more {
      font-size: 24rpx;
      color: $color;
    }
  }
  .months_content {
    display: flex;
    flex-wrap: wrap;
    .months_item {
      width: 33.33%;
      border: 5rpx solid #fff;
    }
  }
}
.hots_wrap {
  .hots_title {
    padding: 20rpx;
    text {
      border-left: 20rpx solid $color;
      padding-left: 20rpx;
      font-size: 34rpx;
      font-weight: 600;
    }
  }
  .hots_content {
    display: flex;
    flex-wrap: wrap;
    .hots_item {
      width: 33.33%;
      border: 5rpx solid #fff;
    }
  }
}
</style>